from flask import Flask, render_template
from util import log, loadSettings, init
import urllib2
import json, time
from ranker import rank

app = Flask(__name__)
app.debug = True

@app.route("/")
def hello():
    return "hello world"

URL_SEARCH_EXPERT = "http://arnetminer.org/services/search-expert?u=clockwise&start=0&num=100&q=%s"
URL_SEARCH_PUBLICATION = "http://arnetminer.org/services/search-publication?u=clockwise&start=0&num=100&q=%s"
URL_SEARCH_CONFERENCE = "http://arnetminer.org/services/search-conference?u=clockwise&start=0&num=100&q=%s"
URL_SEARCH_PUBLICATION_BY_AUTHOR = "http://arnetminer.org/services/publication/byperson/%s"

@app.route("/search/<query>")
def search(query):
    begin = time.time()
    if ' ' in query:
        query = query.replace(' ', '%20')
    log('info', 'query:"%s"' % query)
    content = json.dumps(rank(query))
    log('final', content)
    log('response_time', str(time.time() - begin))
    return content

if __name__ == "__main__":
    init()
    settings = loadSettings()
    ip = settings.get('ip')
    port = int(settings.get('port'))
    app.run(host = ip, port = port, debug=True)
